﻿
/*
 * PsTFS is a set of PowerShell commands to manage and use Team Foundation Server. PowerShell 
 * effectiveness is combined with the power of TFS API to maximizes TFS capabilities.
 * 
 * Creator : Choulant Noham
 * Mail : choulant@gmail.com
 * Blog : http://choulant.blogspot.com
 * 
 */

using System;
using System.Management.Automation;
using Microsoft.TeamFoundation.Build.Client;
using Microsoft.TeamFoundation.Client;
using PsTFS.Methodes;
using PsTFS.Provider;
using PsTFS.Objects;

namespace PsTFS.Commandes.Add
{
    /// <summary>
    /// Ajout d'une qualité pour les build
    /// </summary>
    [Cmdlet(VerbsCommon.Add, "QualityBuild")]
    public class Add_QualityBuild : PsCmdlet
    {
        /// <summary>
        /// Constructeur
        /// </summary>
        public Add_QualityBuild()
        {
        }
        /// <summary>
        /// Permet de lancer le processus de création de qualité
        /// </summary>
        /// <exception cref="ArgumentException">Nom du serveur TFS non valide</exception>
        /// <exception cref="ArgumentException">Nom du projet non valide</exception>
        protected override void ProcessRecord()
        {
           
            if (Quality == null)
            {
                WriteError(new ErrorRecord(new ArgumentException("No quality!!"), "PsTFS", ErrorCategory.ObjectNotFound, Quality));
                return;
            }

            try
            {
                IBuildServer buildServer = Methode_Service.IBS();

                buildServer.AddBuildQuality(Context.Instance.Project.Name, this.Quality);

                Console.WriteLine("Quality : " + this.Quality + " Succes !");

            }
            catch (Exception ex)
            {
                ErrorRecord err = new ErrorRecord(ex, Error.GetInstance().GetError(this), ErrorCategory.WriteError, null);
                WriteError(err);
                Log.AddLog(ex.StackTrace);
            }
            finally
            {
                base.ProcessRecord();
            }
        }

        /// <summary>
        /// Fin du processus
        /// </summary>
        protected override void EndProcessing()
        {
            Log.AddLog(null, "Add-QualityBuild is end");
            base.EndProcessing();
        }
        /// <summary>
        /// Object contenant Team Foundation Server
        /// </summary>
        [Parameter(Position = 0)]
        public TfsTeamProjectCollection TFS
        {
            set { Context.Instance.Tfs = value; }
        }
        /// <summary>
        /// Nom du label
        /// </summary>
        [Parameter(Position = 1)]
        public string ProjectName
        {
            set { Context.Instance.Project = Methode.GetProject(value); }
        }

        /// <summary>
        /// Qualité de build à ajouter
        /// </summary>
        [Parameter(Position = 2, Mandatory = true)]
        public string Quality { get; set; }
        
    }
}


